package com.chenyao.bookstore.dataprovider.impl;

import java.sql.SQLException;
import java.util.List;

import com.chenyao.bookstore.dataprovider.IUserDataProvider;
import com.chenyao.bookstore.dataprovider.processor.impl.UserResultProcessorImpl;
import com.chenyao.bookstore.entity.User;
import com.chenyao.bookstore.util.Constants;

public class UserDataProviderImpl extends BaseDataProvider<Integer, User> implements IUserDataProvider {
	@Override
	public User find(final User user) throws Exception {
		String sql = "SELECT * FROM T_USER WHERE USERNAME = ? AND PASSWORD = ?";
		List<User> list = super.find(sql, new String[] { user.getUsername(), user.getPassword() });
		return list.size() == 1 ? list.get(Constants.ZERO) : null;
	}

	@Override
	protected void initResultProcessor() {
		super.resultProcessor = new UserResultProcessorImpl();
	}

	@Override
	public boolean save(final User user) throws SQLException, ClassNotFoundException {
		String sql = "INSERT INTO T_USER VALUES(NULL, ?, ?, DEFAULT)";
		return super.saveOrUpdateOrDelete(sql, new String[] { user.getUsername(), user.getPassword() }) == 1;
	}
}
